package com.nutransit;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.mylibrary.mylibrary.parsing.AsyncTaskInterface;
import com.mylibrary.mylibrary.parsing.AsyncTaskUtility;
import com.nutransit.commoncode.Webservice_parametrs;

public class Fragment_WebView extends Fragment implements Webservice_parametrs
{
	private Context context;
	private Resources resources;
	
	private View rootView;
	private WebView wv_Content;
	
	private String strPageId = "50";
	HashMap<String, String> hashMap;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		rootView = inflater.inflate(R.layout.fragment_webview, container, false);
		
		init_var();
		return rootView;
	}
	private void init_var()
	{
		context = getActivity();
		resources = context.getResources();
		
		strPageId = getArguments().containsKey(TAG_pageid)?getArguments().getString(TAG_pageid):null;
		System.out.println("strPageId: "+strPageId);
		wv_Content = (WebView) rootView.findViewById(R.id.wv_Content);
		if(strPageId!=null)
		{
			hashMap = new HashMap<String, String>();
			hashMap.put(TAG_pageid, strPageId);
			new AsyncTaskUtility(context, hashMap, URL_GET_PAGES).getJSONResponse(new AsyncTaskInterface()
			{
				@Override
				public void getJSONObjectFromAsynkTask(JSONObject json)
				{
					try
					{
						if(json!=null)
						{
							wv_Content.loadData(json.getJSONArray(TAG_data).getJSONObject(0).getString(TAG_page_content), "text/html", null);
						}
					}
					catch (JSONException e)
					{
						e.printStackTrace();
					}
				}
			});
		}
		else
		{
			wv_Content.loadUrl(getArguments().containsKey(TAG_page_content)?getArguments().getString(TAG_page_content):URL_ABOUT_US);
		}
	}
}
